﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using ProfileManager.DataAccess;
using ProfileManager.Helpers;

namespace ProfileManager.Forms
{
    public partial class AddServiceAccountForm : MasterForm
    {
        public AddServiceAccountForm()
        {
            InitializeComponent();

            ShowCustomLeftButton = false;
            ShowCustomRightButton = false;
        }

        private bool FormValidation()
        {
            bool isValidated = false;

            string accLogin = tbAccountLogin.Text.Trim();
            string accPassword = tbAccountPassword.Text.Trim();
            string accPasswordRepeat = tbAccountRepeatPassword.Text.Trim();
            string accEmail = tbAccountEmail.Text.Trim();
            
            if (!string.IsNullOrEmpty(accLogin) && !string.IsNullOrEmpty(accPassword) && !string.IsNullOrEmpty(accPasswordRepeat))
            {
                if (accPassword.Equals(accPasswordRepeat))
                {
                    isValidated = true;
                }
            }

            if (!string.IsNullOrEmpty(accEmail))
            {
                if (!Regex.Match(accEmail, RegexPatterns.PatternEmail).Success)
                {
                    isValidated = false;
                }
            }

            return isValidated;
        }

        private void AddServiceAccountForm_ClickOk(object sender, EventArgs e)
        {
            if (FormValidation())
            {
                AccountInfo accountInfo = new AccountInfo
                    {
                        ServiceInfoId = Guid.Parse(comboServiceInfo.SelectedValue.ToString()),
                        Login = tbAccountLogin.Text.Trim(),
                        Password = tbAccountPassword.Text.Trim(),
                        Email = tbAccountEmail.Text.Trim(),
                        MobilePhone = tbAccountPhone.Text.Trim(),
                        FirstName = tbAccountFirstName.Text.Trim(),
                        LastName = tbAccountLastName.Text.Trim(),
                        SecretQuestion = tbAccountSecretQuestion.Text.Trim(),
                        SecretAnswer = tbAccountSecretAnswer.Text.Trim(),
                        ShortId = tbAccountShortId.Text.Trim(),
                        AdditionalInfo = tbAccountAdditionalInfo.Text.Trim()
                    };
                accountInfo.Insert();

                if (accountInfo.Id != Guid.Empty)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void AddServiceAccountForm_Load(object sender, EventArgs e)
        {
            FillServiceTypeList();
        }

        private void comboSeviceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillServiceInfoList();
        }

        private void FillServiceTypeList()
        {
            comboSeviceType.DataSource = ServiceType.Select();
        }

        private void FillServiceInfoList()
        {
            comboServiceInfo.DataSource = ServiceInfo.SelectByServiceTypeId(Guid.Parse(comboSeviceType.SelectedValue.ToString()));
        }

        private void bAddServiceType_Click(object sender, EventArgs e)
        {
            AddServiceTypeForm addServiceTypeForm = new AddServiceTypeForm();
            if (addServiceTypeForm.ShowDialog() == DialogResult.OK)
            {
                FillServiceTypeList();
            }
        }

        private void bAddServiceInfo_Click(object sender, EventArgs e)
        {
            Guid selectedServiceTypeId = Guid.Parse(comboSeviceType.SelectedValue.ToString());

            AddServiceInfoForm addServiceInfoForm = new AddServiceInfoForm();

            if (comboSeviceType.SelectedValue != null)
            {
                addServiceInfoForm.SelectedServiceTypeId = selectedServiceTypeId;
            }

            if (addServiceInfoForm.ShowDialog() == DialogResult.OK)
            {
                FillServiceInfoList();
            }
        }
    }
}
